<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2021/5/19
 * Time: 14:11
 */

namespace App\Services\Project;

use Carbon\Carbon;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Grid\Tools\BatchActions;
use Illuminate\Database\Eloquent\Builder;

class ProjectInspectService
{
    public function gridColumn(\Encore\Admin\Grid $grid, $type = null)
    {
        $that = $this;
        $grid->model()->with('project', 'plan');
        $grid->model()->when($type == 1, function (Builder $query) {
            $query->whereIn('state', [0, 2, 3]);
        })->whereHas('plan', function (Builder $query) use ($type) {
            $query->where('state', 1)
                ->where('type', request()->project_type)
                ->when($type == 1, function (Builder $query) {
                    $query->whereDate('end_time', '>=', Carbon::now());
                });
        })->when(request()->plan_id, function ($query) {
            $query->where('project_inspect_id', request()->plan_id);
        })->when(request()->project_id, function ($query) {
            $query->where('project_id', request()->project_id);
        });
        $grid->column('plan.name', __('中检计划'));
        $grid->column('project.name', __('项目名称'))->modal('详情', function ($obj) use ($that) {
            return ProjectService::detail($obj->project);
        });
        $grid->column('plan.start_time', __('中检开始时间'));
        $grid->column('plan.end_time', __('中检开始结束'));
        $grid->column('file', __('中检报告'))->downloadable();
        $grid->column('state', __('中检状态'))
            ->using([0 => '未申请', 3 => '待审核', 1 => '审核已通过', 2 => '审核未通过'])
        ->label(['','success','default','warning']);
        $grid->selector(function ($selector) {
            $selector->select('state', '中检状态', [0 => '未申请', 3 => '待审核', 1 => '审核已通过', 2 => '审核未通过']);
        });
        $grid->quickSearch('project.name', 'plan.name')->placeholder('项目名称/中检计划');
        $grid->disableCreateButton();
        $grid->disableFilter();
        $grid->batchActions(function (BatchActions $actions) {
            $actions->disableDeleteAndHodeSelectAll();
        });
        if($type){
            $grid->actions(function (Actions $actions) {
                $actions->disableDelete();
                $actions->disableView();
                if (Carbon::now()->lt($actions->row->plan->start_time)) {
                    $actions->disableEdit();
                }
            });
        }
        return $grid;
    }
}